home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / System7 tools / D / DarkSide of the Mac 3.1.1.cpt / DarkSide of the Mac 3.1.1 / FaderShell / Fader.c next >
Text File  |  1992-06-07  |  7KB  |  278 lines

  1. /*
  2.     DarkSide 3.0 - a 7.0 dependant, system clean expandable screen saver.
  3.     
  4.     copyright © 1990, 1991, 1992 by Tom Dowdy
  5.     All rights reserved.
  6.     
  7.     This fader shell serves to dispatch requests from the main DarkSide code
  8.     into the appropriate entry points.
  9. */
  10. #include <StdArg.h>
  11. #include <Memory.h>
  12. #include <Packages.h>
  13. #include <Errors.h>
  14.  
  15. #include "Fader.h"
  16.  
  17.  
  18. /* ------------------------------------------------------------------------    */
  19. /* GLOBAL HANDLING ROUTINES */
  20. /* ------------------------------------------------------------------------    */
  21. OSErr    CreateA5World(Ptr * a5World);
  22. void    DisposeA5World(Ptr a5World, Ptr appA5);
  23. void    DebugLongInt(long aLong);
  24.  
  25. /* ------------------------------------------------------------------------    */
  26. OSErr    FaderEntry(short selector, Ptr *a5World, MachineInfoPtr machineInfo, ...)
  27. {
  28.     OSErr    anErr;
  29.     va_list    nextArg;
  30.     
  31.  
  32.         
  33.     // start stripping optional arguments
  34.     va_start(nextArg, machineInfo);
  35.     
  36.     switch(selector)
  37.         {
  38.         case preflightFader:
  39.             {
  40.             long    *minTicks, *maxTicks;
  41.             
  42.             minTicks = va_arg(nextArg, long*);
  43.             maxTicks = va_arg(nextArg, long*);
  44.             
  45.             anErr = CreateA5World(a5World);
  46.             if (anErr == noErr)
  47.                 {
  48.                 (void) SetA5((long) *a5World);
  49.                 BlockMove(machineInfo->applicationQD, &qd, sizeof(qd));
  50.                 anErr = PreflightFader(machineInfo, minTicks, maxTicks);
  51.                 BlockMove(&qd, machineInfo->applicationQD, sizeof(qd));
  52.                 }
  53.             }
  54.             break;
  55.             
  56.         case initializeFader:
  57.             (void) SetA5((long) *a5World);
  58.             BlockMove(machineInfo->applicationQD, &qd, sizeof(qd));
  59.             anErr = InitializeFader(machineInfo);
  60.             BlockMove(&qd, machineInfo->applicationQD, sizeof(qd));
  61.             break;
  62.             
  63.         case idleFader:
  64.             (void) SetA5((long) *a5World);
  65.             anErr = IdleFader(machineInfo);
  66.             break;
  67.             
  68.         case disposeFader:
  69.             {
  70.             
  71.             (void) SetA5((long) *a5World);
  72.             BlockMove(machineInfo->applicationQD, &qd, sizeof(qd));
  73.             anErr = DisposeFader(machineInfo);
  74.             BlockMove(&qd, machineInfo->applicationQD, sizeof(qd));
  75.             DisposeA5World(*a5World, (Ptr)machineInfo->applicationA5);
  76.             
  77.             }
  78.             break;
  79.  
  80.         case updateFader:
  81.             (void) SetA5((long) *a5World);
  82.             anErr = UpdateFader(machineInfo);
  83.             break;
  84.             
  85.         case hitFader:
  86.             {
  87.             DialogPtr    dPtr;
  88.             long        itemHit;
  89.             long        itemOffset;
  90.                         
  91.             dPtr         = va_arg(nextArg, DialogPtr);
  92.             itemHit     = va_arg(nextArg, long);
  93.             itemOffset     = va_arg(nextArg, long);
  94.  
  95.             anErr = HitFader(machineInfo, dPtr, itemHit, itemOffset);            
  96.             }
  97.             
  98.             break;
  99.             
  100.         default:
  101.             // function not found error
  102.             anErr = fnfErr;
  103.             break;
  104.         }
  105.         
  106.     va_end(nextArg);
  107.     return(anErr);    
  108.     
  109. } // FaderEntry
  110.  
  111. /* ------------------------------------------------------------------------    */
  112. /* DEBUGGING ROUTINES                             */
  113. /* ------------------------------------------------------------------------    */
  114. void DebugLongInt(long theLong)
  115. {
  116.     Str255 theString;
  117.     
  118.     NumToString(theLong, theString);
  119.     DebugStr(theString);
  120.     
  121. } // DebugLongInt
  122.  
  123. /* ------------------------------------------------------------------------    */
  124. /* FUN A5 STUFF - See Tech note 256 for details                             */
  125. /* ------------------------------------------------------------------------    */
  126. long A5Size(); 
  127. void A5Init(Ptr theA5);
  128.  
  129. OSErr    CreateA5World(Ptr * a5World)
  130. {
  131.     OSErr    anErr;
  132.     Ptr        theWorld = nil;
  133.     Handle    worldHandle;
  134.     
  135.     worldHandle = BestNewHandle(A5Size());
  136.     anErr = MemError();
  137.     if (anErr == noErr)
  138.         {
  139.         MoveHHi(worldHandle);
  140.         HLock(worldHandle);
  141.         theWorld = *worldHandle;
  142.         
  143.         theWorld += + A5Size() - 32;
  144.         A5Init(theWorld);
  145.         
  146.         // very important if anyone wants to call SwapMMUMode
  147.         theWorld = StripAddress(theWorld);
  148.         }
  149.     *a5World = theWorld;
  150.     
  151.     return(anErr);
  152.     
  153. } // CreateA5World
  154.  
  155. /* ------------------------------------------------------------------------    */
  156. void    DisposeA5World(Ptr a5World, Ptr appA5)
  157. {
  158.     Handle    worldHandle;
  159.     
  160.     (void) SetA5((long) appA5);
  161.     
  162.     worldHandle = RecoverHandle(a5World - A5Size() + 32);
  163.     DisposHandle(worldHandle);
  164.     
  165. } // DisposeA5World
  166.  
  167. /* ------------------------------------------------------------------------    */
  168. /* FADER UTILS                                                                 */
  169. /* ------------------------------------------------------------------------    */
  170.  
  171. Handle    BestNewHandle(Size    theSize)
  172. /*
  173.     Tries to get the handle from the temp memory first, if that fails, it goes
  174.     to the application.
  175. */
  176. {
  177.     Handle theHandle;
  178.     OSErr    anErr;
  179.     
  180.     theHandle = TempNewHandle(theSize, &anErr);
  181.     if (theHandle == nil)
  182.         theHandle = NewHandle(theSize);
  183.         
  184.     return(theHandle);
  185.     
  186. } // BestNewHandle
  187.  
  188. /* ------------------------------------------------------------------------    */
  189.  
  190. RgnHandle    BestNewRgn()
  191. /*
  192.     Tries to get a rgn handle from the temp memory first, if that fails, it goes
  193.     to the application.  Needs enough room in the app heap to create the region
  194.     in the first place.
  195. */
  196. {
  197.     RgnHandle     theRgn;
  198.     OSErr        anErr;
  199.     
  200.     // make a region
  201.     theRgn = NewRgn();
  202.     if (theRgn != nil)
  203.         {
  204.         RgnHandle    theHandle;
  205.         short        regionSize;
  206.         
  207.         // try to make something the same size in the temp memory
  208.         regionSize = GetHandleSize((Handle) theRgn);
  209.         theHandle = (RgnHandle) TempNewHandle(regionSize, &anErr);
  210.         if (anErr == noErr)
  211.             {
  212.             // if we get it, use that one for our region
  213.             BlockMove(*theRgn, *theHandle, regionSize);
  214.             DisposeRgn(theRgn);
  215.             theRgn = theHandle;
  216.             }
  217.         }
  218.         
  219.     return(theRgn);
  220.     
  221. } // BestNewRgn
  222.  
  223.  
  224.  
  225. /* ------------------------------------------------------------------------    */
  226.  
  227. short    Rnd(long max)
  228. /*
  229.     Returns a number > 0 and < max
  230. */
  231. {
  232. unsigned long value;
  233.  
  234.     value = (unsigned short)max * (unsigned short)Random();
  235.     value >>= 16;
  236.     return(value);
  237.     
  238. } // Rnd
  239.  
  240. /* ------------------------------------------------------------------------    */
  241. void PlaceRectOnScreen(
  242.     MachineInfoPtr machineInfo,    // give info about the machine here
  243.     short width,                // width of rect, can be 0
  244.     short height,                // height of rect, can be 0
  245.     Rect * placedRect,            // Placed rect is returned here
  246.     Rect * margins,                // margins around screen, can be nil
  247.     short * whichScreen)        // screen index returned here, can be nil
  248. {
  249.     Rect        screenRect;
  250.     short        pickScreen;
  251.  
  252.     // pick a random screen    
  253.     pickScreen = Rnd(machineInfo->numScreens);
  254.     screenRect = machineInfo->theScreens[pickScreen].bounds;
  255.     if (whichScreen != nil)
  256.         *whichScreen = pickScreen;
  257.         
  258.     if (margins != nil)
  259.         {
  260.         screenRect.top += margins->top;
  261.         screenRect.left += margins->left;
  262.         screenRect.bottom -= margins->bottom;
  263.         screenRect.right -= margins->right;
  264.         }
  265.         
  266.     screenRect.right -= width;
  267.     screenRect.bottom -= height;
  268.     
  269.     if (placedRect != nil)
  270.         {
  271.         placedRect->top = screenRect.top + Rnd(screenRect.bottom - screenRect.top);
  272.         placedRect->left = screenRect.left + Rnd(screenRect.right - screenRect.left);
  273.         placedRect->bottom = placedRect->top + height;
  274.         placedRect->right = placedRect->left + width;
  275.         }
  276.         
  277. } // PlaceRectOnScreen
  278.